package com.softserveinc.moviedbapp.customview;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.softserveinc.moviedbapp.R;
import com.softserveinc.moviedbapp.customview.MovieTheaterView.OnDrawingViewListener;
import com.softserveinc.moviedbapp.map.CinemaInfoProvider;
import com.softserveinc.moviedbapp.util.MovieAppConstants;

import java.util.Currency;
import java.util.HashMap;

public class MovieTheaterBookingActivity extends Activity implements OnDrawingViewListener {

	private static final String TAG = MovieTheaterBookingActivity.class.getSimpleName();
	private MovieTheaterView mDrawingView;
	private TextView mCurrentPrice;
	private String mCurrencySymbol;

	/** Called when the activity is first created. */
	@Override
	public void onCreate( final Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
		Log.d( TAG, "onCreate()" );

		CinemaInfoProvider.sCinemaId = (Integer) getIntent().getExtras().get( MovieAppConstants.CINEMA_ID );

		setContentView( R.layout.movie_theater_view );

		mCurrencySymbol = Currency.getInstance( getResources().getConfiguration().locale ).getSymbol();

		mDrawingView = (MovieTheaterView) findViewById( R.id.SeatsView );
		mDrawingView.setFocusable( true );

		if ( mDrawingView == null ) {
			Log.d( TAG, "onCreate() drawingView is null" );
		} else {
			Log.d( TAG, "onCreate() drawingView is not null" );
		}

		mCurrentPrice = (TextView) findViewById( R.id.price_view );
		mCurrentPrice.setText( this.getResources().getString( R.string.current_price_ ) + " 0.00" + mCurrencySymbol );

		Button doneButton = (Button) findViewById( R.id.book_seats_button );

		doneButton.setOnClickListener( new OnClickListener() {

			@Override
			public void onClick( final View v ) {
				CinemaInfoProvider.saveSeatMap( MovieTheaterBookingActivity.this, mDrawingView.getBookingMap(),
						mDrawingView.getCurrentSessionBookingMap() );
				finish();
			}
		} );
	}

	@Override
	public HashMap<Integer, Boolean> onSeatsUpdate( final int seatCount ) {

		return CinemaInfoProvider.readSeatMap( this, seatCount );
	}

	@Override
	public void onPriceChanged( final double price ) {
		mCurrentPrice.setText( this.getResources().getString( R.string.current_price_ )
				+ String.format( " %.2f%s", price, mCurrencySymbol ) );

	}

}